<template>
    <div class="box5">
        <div class="btnBox">
            <el-button type="danger" class="lookclasstable" @click="check" :disabled="checkFlag">检查</el-button>
            <span class="tip">请先检查再提交</span>
            <el-button class="btn" type="success" @click="updataCourse" :disabled="submitFlag">完成</el-button>
            <a href="#" @click="$router.push('/classmanager/classdetails/courseinformation/timetable?clazzId='+$route.query.clazzId+'&clazzName='+$route.query.clazzName)">返回</a>
        </div>
        <div class="wrap">
            <h4>{{$route.query.clazzName}}课程表</h4>
            <el-table border :data="tableData" @cell-click="mouse">
                <el-table-column label="" prop="level"></el-table-column>
                <el-table-column label="周一">
                    <template slot-scope="scope">
                        <el-select v-model="scope.row.list[0].subId" @change="getcourseid(scope.row.list[0])" placeholder="原科目">
                            <el-option v-for="item in courseslist" :key="item.subId" :label="item.subName"
                                :value="item.subId">
                            </el-option>
                        </el-select>
                        <el-select  :class='{active:scope.row.list[0].myErrorData.findIndex(v=>v=="周1"+scope.row.list[0].timeName) != -1 ? true : false}' v-model="scope.row.list[0].teaId" placeholder="原任课老师">
                            <el-option v-for="item in scope.row.list[0].teacherlist" :key="item.teaId" :label="item.teaName"
                                :value="item.teaId">
                            </el-option>
                        </el-select>
                    </template>
                </el-table-column>
                <el-table-column label="周二">
                    <template slot-scope="scope">
                        <el-select v-model="scope.row.list[1].subId" @change="getcourseid(scope.row.list[1])" placeholder="原科目">
                            <el-option v-for="item in courseslist" :key="item.subId" :label="item.subName"
                                :value="item.subId">
                            </el-option>
                        </el-select>
                        <el-select  :class='{active:scope.row.list[1].myErrorData.findIndex(v=>v=="周2"+scope.row.list[1].timeName) != -1 ? true : false}' v-model="scope.row.list[1].teaId" placeholder="原任课老师">
                            <el-option v-for="item in scope.row.list[1].teacherlist" :key="item.teaId" :label="item.teaName"
                                :value="item.teaId">
                            </el-option>
                        </el-select>
                    </template>
                </el-table-column>
                <el-table-column label="周三">
                    <template slot-scope="scope">
                        <el-select v-model="scope.row.list[2].subId" @change="getcourseid(scope.row.list[2])" placeholder="原科目">
                            <el-option v-for="item in courseslist" :key="item.subId" :label="item.subName"
                                :value="item.subId">
                            </el-option>
                        </el-select>
                        <el-select  :class='{active:scope.row.list[2].myErrorData.findIndex(v=>v=="周3"+scope.row.list[2].timeName) != -1 ? true : false}' v-model="scope.row.list[2].teaId" placeholder="原任课老师">
                            <el-option v-for="item in scope.row.list[2].teacherlist" :key="item.teaId" :label="item.teaName"
                                :value="item.teaId">
                            </el-option>
                        </el-select>
                    </template>
                </el-table-column>
                <el-table-column label="周四">
                    <template slot-scope="scope">
                        <el-select v-model="scope.row.list[3].subId" @change="getcourseid(scope.row.list[3])" placeholder="原科目">
                            <el-option v-for="item in courseslist" :key="item.subId" :label="item.subName"
                                :value="item.subId">
                            </el-option>
                        </el-select>
                        <el-select  :class='{active:scope.row.list[3].myErrorData.findIndex(v=>v=="周4"+scope.row.list[3].timeName) != -1 ? true : false}' v-model="scope.row.list[3].teaId" placeholder="原任课老师">
                            <el-option v-for="item in scope.row.list[3].teacherlist" :key="item.teaId" :label="item.teaName"
                                :value="item.teaId">
                            </el-option>
                        </el-select>
                    </template>
                </el-table-column>
                <el-table-column label="周五">
                    <template slot-scope="scope">
                        <el-select v-model="scope.row.list[4].subId" @change="getcourseid(scope.row.list[4])" placeholder="原科目">
                            <el-option v-for="item in courseslist" :key="item.subId" :label="item.subName"
                                :value="item.subId">
                            </el-option>
                        </el-select>
                        <el-select  :class='{active:scope.row.list[4].myErrorData.findIndex(v=>v=="周5"+scope.row.list[4].timeName) != -1 ? true : false}' v-model="scope.row.list[4].teaId" placeholder="原任课老师">
                            <el-option v-for="item in scope.row.list[4].teacherlist" :key="item.teaId" :label="item.teaName"
                                :value="item.teaId">
                            </el-option>
                        </el-select>
                    </template>
                </el-table-column>
                <el-table-column label="周六">
                    <template slot-scope="scope">
                        <el-select v-model="scope.row.list[5].subId" @change="getcourseid(scope.row.list[5])" placeholder="原科目">
                            <el-option v-for="item in courseslist" :key="item.subId" :label="item.subName"
                                :value="item.subId">
                            </el-option>
                        </el-select>
                        <el-select  :class='{active:scope.row.list[5].myErrorData.findIndex(v=>v=="周6"+scope.row.list[5].timeName) != -1 ? true : false}' v-model="scope.row.list[5].teaId" placeholder="原任课老师">
                            <el-option v-for="item in scope.row.list[5].teacherlist" :key="item.teaId" :label="item.teaName"
                                :value="item.teaId">
                            </el-option>
                        </el-select>
                    </template>
                </el-table-column>
                <el-table-column label="周日">
                    <template slot-scope="scope">
                        <el-select v-model="scope.row.list[6].subId" @change="getcourseid(scope.row.list[6])" placeholder="原科目">
                            <el-option v-for="item in courseslist" :key="item.subId" :label="item.subName"
                                :value="item.subId">
                            </el-option>
                        </el-select>
                        <el-select  :class='{active:scope.row.list[6].myErrorData.findIndex(v=>v=="周日"+scope.row.list[6].timeName) != -1 ? true : false}' v-model="scope.row.list[6].teaId" placeholder="原任课老师">
                            <el-option v-for="item in scope.row.list[6].teacherlist" :key="item.teaId" :label="item.teaName"
                                :value="item.teaId">
                            </el-option>
                        </el-select>
                    </template>
                </el-table-column>
            </el-table>
        </div>
    </div>
</template>

<script>
export default {
    data() {
        return {
            baseUrl: require("../js/baseUrl.js").baseUrl,
            tableData: [],
            pageIndex: 1,
            pageSize: 20,
            teacherlist: [],
            courseslist: [],
            schas: [],
            total: "",
            submitFlag: true,
			checkFlag:true,
            myErrorData:[]
        }
    },
    methods: {
        //检查
		check() {
			//前端验证
			
            let schas = []
            this.tableData.map(v => {
                // console.log(v.list);
                v.list.map(item => {
                    // console.log(item);
                    delete item.courseName
                    delete item.courseTea
                    delete item.weekIndex
                    // console.log(item);
                    schas.push(item)
                })
            })
            this.schas = schas
			// console.log("schas",this.schas)
			//调用检查接口
			//SchoolSys/scha/check
			this.$http.post(this.baseUrl + "SchoolSys/scha/check", {
				schas: this.schas
			}).then(res => {
				if (res.data.code == 200) {
					this.checkFlag=true
					this.submitFlag = false
					//console.log(this.myErrorData);
					this.myErrorData=[]
					this.tableData.forEach(v=>{
						v.list.forEach(item=>{
							item.myErrorData=this.myErrorData
						})
					})
					//console.log(this.myErrorData);
					this.$message.success("检查完成，请提交")
				}else if(res.data.code==500){
					this.$message.error("教师存在上课冲突，请调整")
					this.myErrorData=[]
					let errorData=res.data.data
					errorData.forEach(v=>{
						this.myErrorData.push(v.dayName+v.timeName)
					})
					// console.log("检查返回",this.myErrorData);
					//console.log('table',this.tableData)
					this.tableData.forEach(v=>{
						v.list.forEach(item=>{
							item.myErrorData=this.myErrorData
						})
					})

				}
			})
		},
        change() {
            this.$forceUpdate()
        },
        //获取课程表
        getCourses() {
            this.checkFlag=false;
            this.$http.post(this.baseUrl + 'SchoolSys/scha/searchByIClazz', {
                clazzId: Number(this.$route.query.clazzId),
            }).then(res => {
                // console.log(res);
                if (res.data.code == 200) {
                    let resData = res.data.data
                    //  console.log(resData)
                    let datas = []
                    for (var i in resData) {
                        // console.log(resData[i])
                        let list = [];
                        for (var k in resData[i]) {
                            list.push({
                                courseName: resData[i][k].subName,
                                courseTea: resData[i][k].teaName,
                                schaId: resData[i][k].schaId,
                                teaId:resData[i][k].teaId,
                                subId:resData[i][k].subId,
                                clazzId:Number(this.$route.query.clazzId) ,
                                dayName: k,
                                weekIndex: k.substring(1, 2),
                                teacherlist:[],
                                timeName:'',
                                myErrorData:[]
                            })
                        }
                        //排序list
                        for (var m = 0; m < list.length; m++) {
                            for (var k = m + 1; k < list.length; k++) {
                                if (list[m].weekIndex > list[k].weekIndex) {
                                    let temp = list[m]
                                    list[m] = list[k]
                                    list[k] = temp
                                }
                            }
                        }

                        datas.push({
                            index: i.substring(1, 2),
                            level: i,
                            list: list
                        })
                    }
                    // console.log(datas)

                    for (var i = 0; i < datas.length; i++) {
                        for (var k = i + 1; k < datas.length; k++) {
                            if (datas[i].index > datas[k].index) {
                                let temp = datas[i]
                                datas[i] = datas[k]
                                datas[k] = temp
                            }
                        }
                    }
                    
                    this.tableData = datas
                    // console.log('课程表',this.tableData)
                    this.tableData.map(v=>{
                        // console.log(v.level);
                        v.list.map(item=>{
                            item.timeName=v.level
                            this.$http.post(this.baseUrl + "SchoolSys/getTeachersBySubId", {
								subId:item.subId
							}).then(res => {
								//console.log("777",res);
								if(res.data.code==200){
									item.teacherlist=res.data.data.teachers
                                    item.myErrorData=[]
                                    // console.log(item.timeName);
								}
							})
                        })
                    })
                }
            })
        },
        //修改课程表
        updataCourse() {
            // this.getSchas()
            this.$http.post(this.baseUrl + "SchoolSys/scha/update", {
                schas: this.schas,
            }).then(res => {
                // console.log(res);
                this.$message.success('修改成功')
                this.$router.push('/classmanager/classdetails/courseinformation?clazzId='+this.$route.query.clazzId+'&clazzName='+this.$route.query.clazzName)
            })
        },
        //获取schas列表
       
        //根据班级id获取科目
        getcourse() {
            this.$http.post(this.baseUrl+'SchoolSys/clazz/selectclazzsub',{
                clazzId:Number(this.$route.query.clazzId) 
            }).then(res=>{
                // console.log(res.data.data);
                if(res.data.code==200){
                    this.courseslist=res.data.data.filter(v=>v.status==1)
                }
            })
        },
        //根据课程id查询老师：
        getcourseid(Coursesid){
            // console.log(Coursesid);
			this.$http.post(this.baseUrl + "SchoolSys/getTeachersBySubId", {
				subId:Coursesid.subId
			}).then(res => {
				if(res.data.code==200){
					// this.teacherlist=res.data.data.teachers
                    Coursesid.teacherlist=res.data.data.teachers
                   
				}
			})
        },
    },
    mounted() {
        this.getCourses()
        this.getcourse()
    }
}
</script>

<style lang="less">
.box5 {
    .el-select.active input{
        border: 1px solid red;
    }
    .el-input__inner {
        height: 25px;
        line-height: 25px;

    }

    .el-input__icon {
        line-height: 0;
    }
    // position: relative;
	.wrap {
		padding: 20px;
		margin-top: 60px;
		background: #fff;

		h4 {
			text-align: center;
			padding: 0 0 15px;
		}

		.el-table td.el-table__cell,
		.el-table th.el-table__cell.is-leaf {
			border-bottom: 1px solid #333;
		}

        .el-input__inner:nth-of-type(1) {
			margin-top: 8px
		}
		.el-table--border,
		.el-table--group {
			border: 1px solid #333;
		}

		.el-table--border .el-table__cell,
		.el-table__body-wrapper .el-table--border.is-scrolling-left~.el-table__fixed {
			border-right: 1px solid #333;
		}

		.tab .el-table__body {
			border-spacing: 0 0;
		}
	}

	.el-table th.el-table__cell {
		background: #fff !important;
	}
	.btnBox {
		position: absolute;
        padding-bottom: 15px;
		top: 40px;
		right: 40px;

		.btn {
			margin: 0 20px;
		}
        .tip {
            position: absolute;
            bottom: 0;
            left: 0;
            font-size: 10px;
            color: #ccc;
        }
	}
}</style>